﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Presentation.Module.Security.Interfaces.ViewModels;
using Presentation.Module.Security.Interfaces.Services;
using Presentation.Module.Security.Model;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using Presentation.Module.Security.Interfaces.Views;
using Microsoft.Practices.Prism.Regions;
using Presentation.Shell.Interfaces;
using System.Collections.ObjectModel;
using System.Windows;
using System.Web.Security;

namespace Presentation.Module.Security.ViewModels
{
    public class AddUserViewModel : UserViewModelBase, IAddUserViewModel, INotifyPropertyChanged 
    {
       

        private User _currentUser;
        private Role _currentRole;

        public ICommand AddUser { get; private set; }

        public AddUserViewModel(IRegionManager regionManager, IUserService userService)
            : base(regionManager, userService)
        {
            this.AddUser = new DelegateCommand(this.OnAddUser);

            _currentUser = new User();
            _currentRole = new Role();

        }

        public User CurrentUser
        {
            get
            {
                return this._currentUser;
            }

            set
            {
                if (value == null)
                {
                    return;
                }

                this._currentUser = value;
                this.RaiseOnPropertyChanged("CurrentUser");
            }
        }

        public string RepeatPassword { get; set; }
        public string FirstPassword { get; set;  }

        public Role CurrentRole
        {
            get { return this._currentRole; }

            set
            {
                if(value == null)
                {
                    return;
                }

                this._currentRole = value;
               // this.RaiseOnPropertyChanged("CurrentRole");
            }
        }

        public void OnAddUser()
        {   
            if(AddingAUser != null)
            {
                AddingAUser.Invoke();
            }

            if (_currentUser == null || _currentRole == null)
                return;

            if (FirstPassword != RepeatPassword)
            {
                MessageBox.Show("Please ensure that your repeat password matches the first", "Passsword Error",
                                MessageBoxButton.OK);
                return;
            }

            if (!this._currentUser.UserPassword.ChangePassword(FirstPassword))
            {
                MessageBox.Show("Please ensure that your password is between 5 and 50 characters long, contains at least one digit. Password can also contain these characters: !@#$%^&()_+{}|:<>?\"[]\\;',./", "Passsword Error", MessageBoxButton.OK);
                return;
            }
            this._currentUser.UserRole = _currentRole;
            this._userService.AddUser(this._currentUser);
            this._currentUser = new User();
            
            this.ExitView();
        }

        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void ExitView()
        {
            if (this.Exit != null)
            {
                this.Exit.Invoke();
            }

        }

        public ObservableCollection<Role> Roles
        {
            get
            {
                //var listRoles = this._userService.GetAllRoles();
                //var stringRoles = new ObservableCollection<string>();
                //foreach (var role in listRoles)
                //{
                //    stringRoles.Add(role.Name);
                //}
                //return stringRoles;
                return this._userService.GetAllRoles();
            }
        }

        public event ViewExit Exit;

        public IAddUserView View { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public event ButtonPressed AddingAUser;

        public string HashPassword(string password)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(password, "sha1");
        }
    }
}
